package com.zlkj.pro.modular.business.controller;

import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserRoleService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import cn.stylefeng.roses.kernel.system.modular.user.service.impl.SysUserServiceImpl;
import com.zlkj.pro.core.consts.ProjectConstants;
import com.zlkj.pro.core.exception.BusinessException;
import com.zlkj.pro.modular.admin.entity.User;
import com.zlkj.pro.modular.admin.mapper.UserMapper;
import com.zlkj.pro.modular.admin.model.dto.UserDTO;
import com.zlkj.pro.modular.admin.model.vo.UserVO;
import com.zlkj.pro.modular.app.service.AppUsersService;
import com.zlkj.pro.modular.business.service.MessageService;
import com.zlkj.pro.modular.business.service.WebUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zlkj.pro.core.consts.ProjectConstants.SERVICE_API_PREFIX;
import static com.zlkj.pro.core.exception.enums.BusinessExceptionEnum.BUSINESS_EXCEPTION;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiResource(name = "用户", path = SERVICE_API_PREFIX + "/web/user")
public class WebUserController {

    @Autowired
    private final AppUsersService appUsersService;

    @Autowired
    private WebUserService webUserService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private final MessageService messageService;


    @GetResource(name = "代理商员工列表", path = "/getAgentUserList")
    public ResponseData getAgentUserList(UserDTO dto) {
        // 获取当前登录用户信息
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<UserVO> userVOs = appUsersService.getAgentUserList(dto);
        return new SuccessResponseData(userVOs);
    }

    @GetResource(name = "代理商员工列表", path = "/getAgentUserListByAgentId")
    public ResponseData getAgentUserListByAgentId(UserDTO dto) {
        List<UserVO> userVOs = appUsersService.getAgentUserListByAgentId(dto);
        return new SuccessResponseData(userVOs);
    }


    @GetResource(name = "代理商员工列表", path = "/getAgentUserListByRoleId")
    public ResponseData getAgentUserListByRoleId(UserDTO dto) {
        List<UserVO> userVOs = appUsersService.getAgentUserListByRoleId(dto);
        return new SuccessResponseData(userVOs);
    }

    @GetResource(name = "修改用户信息", path = "/updateUserInfo")
    public ResponseData updateUserInfo(UserDTO dto) {
        // 获取当前登录用户信息
        //Long userId = LoginContext.me().getLoginUser().getUserId();
        //dto.setUserId(userId);
        SysUser updateSysUserNow = sysUserService.getById(dto.getSelectUserId());
        User updateSysUser = new User();
        updateSysUser.setUserId(dto.getSelectUserId());
        updateSysUser.setAgentId(dto.getAgentId());
        updateSysUser.setRegion(dto.getRegion());
        updateSysUser.setStatusFlag(updateSysUserNow.getStatusFlag());
        Boolean result = webUserService.updateById(updateSysUser);
        return new SuccessResponseData(result);
    }


    @GetResource(name = "代理商员工列表", path = "/getUserInfo")
    public ResponseData getUserInfo(UserDTO dto) {
        UserVO userVO = appUsersService.getWebUserInfo(dto.getUserId());
        return new SuccessResponseData(userVO);
    }

    @GetResource(name = "角色下拉", path = "/sysRole/dropDown", responseClass = SimpleDict.class)
    public ResponseData dropDown() {
        return new SuccessResponseData(webUserService.dropDown());
    }

    @GetResource(name = "分页查询", path = "/findPage")
    public ResponseData findPage(UserDTO dto) {
        PageResult<UserVO> restData = webUserService.findPage(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "用户管理-新增", path = "/addUser")
    public ResponseData addUser(@RequestBody UserDTO dto) {
        boolean restData = webUserService.addUser(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "用户管理-修改", path = "/editUser")
    public ResponseData editUser(@RequestBody UserDTO dto) {
        boolean restData = webUserService.editUser(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "未读消息计数", path = "/getUserUnReadCount")
    public ResponseData getUserUnReadCount() {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        Integer count = messageService.countUnread(userId);// 消息计数
        return new SuccessResponseData(count);
    }


    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private UserMapper userMapper;

    @PostResource(name = "系统用户_修改状态", path = "/sysUser/changeStatus")
    public ResponseData changeStatus(@RequestBody @Validated(SysUserRequest.changeStatus.class) SysUserRequest sysUserRequest) {
        LoginUser currentUser = LoginContext.me().getLoginUser();

        List<Long> userRole = sysUserRoleService.findRoleIdsByUserId(currentUser.getUserId());

        User user = userMapper.selectById(sysUserRequest.getUserId());
        if (sysUserRequest.getStatusFlag() == 1 && user.getStatusFlag() == 2
                && !userRole.contains(ProjectConstants.ROLE_ID_BMSH_MANAGER)
                && !currentUser.getSuperAdmin()) {
            // 请联系BMSH管理者
            throw new BusinessException(BUSINESS_EXCEPTION, "请联系BMSH管理者！");
        }
        sysUserService.editStatus(sysUserRequest);
        return new SuccessResponseData();
    }
}
